<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>品牌管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/css/style.css">
	<script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="/plugins/bootstrap/js/bootstrap.min.js"></script>

    
</head>
<body class="hold-transition skin-red sidebar-mini">
  <!-- .box-body -->
                    <div class="box-header with-border">
                        <h3 class="box-title">品牌管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" onclick="delectInfo()" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                                         
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">品牌ID</th>
									      <th class="sorting">品牌名称</th>									      
									      <th class="sorting">品牌首字母</th>									     				
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="brandList">
			                      </tbody>
			                  </table>
			                  <!--数据列表/-->                        
							  
							 
                        </div>
                        <!-- 数据表格 /-->
                        
                        
                        
                        
                     </div>
                    <!-- /.box-body -->
         
<!-- 增加窗口 -->
<form id="insertForm">
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">品牌编辑</h3>
		</div>
		<div class="modal-body">		
			<table class="table table-bordered table-striped"  width="800px">
		      	<tr>
		      		<td>品牌名称</td>
		      		<td><input  class="form-control" name="name" placeholder="品牌名称" >  </td>
		      	</tr>		      	
		      	<tr>
		      		<td>首字母</td>
		      		<td><input  class="form-control" name="firstChar" placeholder="首字母">  </td>
		      	</tr>		      	
			 </table>				
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" onclick="insert()" data-dismiss="modal" aria-hidden="true">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>
</form>
  <!-- 修改窗口 -->
  <form id="updateForm">
	  <div class="modal fade" id="editModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel1" aria-hidden="true">
		  <div class="modal-dialog" >
			  <div class="modal-content">
				  <div class="modal-header">
					  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
					  <h3 id="myModalLabel1">品牌编辑</h3>
				  </div>
				  <div class="modal-body">
					  <table class="table table-bordered table-striped"  width="800px">
						  <input type="hidden" name="id" id="id">
						  <tr>
							  <td>品牌名称</td>
							  <td><input  class="form-control" id="name" name="name" placeholder="品牌名称" >  </td>
						  </tr>
						  <tr>
							  <td>首字母</td>
							  <td><input  class="form-control" id="firstChar" name="firstChar" placeholder="首字母">  </td>
						  </tr>
					  </table>
				  </div>
				  <div class="modal-footer">
					  <button class="btn btn-success" onclick="updateInfo()" data-dismiss="modal" aria-hidden="true">保存</button>
					  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
				  </div>
			  </div>
		  </div>
	  </div>
  </form>
  <a href="javascript:toPage(3)">首页</a>
  <a href="javascript:toPage(1)">上一页</a>
  <a href="javascript:toPage(2)">下一页</a>
  <a href="javascript:toPage(4)">尾页</a>
  第<input type="text" id="pageNum" name="pageNum" size="2px" >页
  共<input type="text" id="total"  name="total" size="2px" readonly="readonly">条
  总<input type="text" id="pages"  name="pages" size="2px" readonly="readonly">页
  每页<input type="text" id="pageSize"  name="pageSize" size="2px" readonly="readonly">条
<script>
	$(function (){
		brandList();
	})

	function brandList(){
		$.ajax({
			url:"/brand/brandList",
			data:{pageNum:$("#pageNum").val(),pageSize:$("#pageSize").val()},
			type:"post",
			dataType:"json",
			async:false,
			success:function (result){
			console.log(result.data);
			if (result.code == 10000){
				var list = result.data.rows;
				var str = '';
				for (var i = 0; i < list.length; i++){
					str+='<tr>'
					str+='<td><input type="checkbox" name="box" value="'+list[i].id+'"></td>'
					str+='<td>'+list[i].id+'</td>'
					str+='<td>'+list[i].name+'</td>'
					str+='<td>'+list[i].firstChar+'</td>'
					str+='<td className="text-center">'
					str+='<button type="button" onclick="toUpdate('+list[i].id+')" className="btn bg-olive btn-xs" data-toggle="modal"data-target="#editModal1">修改</button>'
					str+='</td>'
					str+='</tr>'
				}
				$("#brandList").html(str);
				pageinstall(result.data);
			}else{
				$("#brandList").html('<tr><td>没数据</td></tr>')
			}
			},error:function(result){
				alert("系统异常");
			}
		})
	}

	function toPage(flag){
		var pageNum = parseInt($("#pageNum").val());
		var pages = parseInt($("#pages").val());
		if (flag == 1){//上一页
			if (pageNum <= 1){
				alert("已经是首页");
				return;
			}
			$("#pageNum").val(pageNum-1);
		}
		else if (flag == 2){//下一页
			if (pageNum >= pages){
				alert("已经是尾页")
				return;
			}
			$("#pageNum").val(pageNum+1);
		}else if(flag == 3){
			$("#pageNum").val(1);
		}else  if (flag == 4){
			$("#pageNum").val(pages);
		}
		brandList();

	}

	function pageinstall(page){
		$("#pageNum").val(page.pageNum);
		$("#pageSize").val(page.pageSize);
		$("#total").val(page.total);
		$("#pages").val(page.pages);
	}

	function insert(){
		$.ajax({
			url:"/brand/insertInfo",
			data:$("#insertForm").serialize(),
			type:"post",
			dataType:"json",
			async:false,
			success:function(result){
				if (result.code == 10000){
					brandList();
				}
			},error:function(result){
				alert("系统异常");
			}
		})
	}

	function toUpdate(id){
		$.ajax({
			url:"/brand/toUpdateById",
			data:{id:id},
			type:"post",
			dataType:"json",
			async:false,
			success:function(result){
				var tbBrand = result.data;
				$("#id").val(tbBrand.id);
				$("#name").val(tbBrand.name);
				$("#firstChar").val(tbBrand.firstChar);
			},error:function(result){
				alert("系统异常");
			}
		})
	}

	function updateInfo(){
		$.ajax({
			url:"/brand/updateInfo",
			data:$("#updateForm").serialize(),
			type:"post",
			dataType:"json",
			async:false,
			success:function(result){
				if (result.code == 10000){
					brandList();
				}
			},error:function(result){
				alert("系统异常");
			}
		})
	}

	function delectInfo(){
		var boxs = document.getElementsByName("box");
		var ids = [];
		for (var i= 0;i<boxs.length;i++){
			if (boxs[i].checked){
				ids[i] = boxs[i].value;
			}
		}
		if (ids.length == 0){
			alert("请选择要删除的选项");
			return;
		}
		$.ajax({
			url:"/brand/deleteInfo",
			data:{"ids":ids},
			type:"post",
			dataType:"json",
			async:false,
			success:function(result){
				if (result.code == 10000){
					brandList();
				}
			},error:function(){
				alert("系统异常");
			}
		})
	}
</script>
   
</body>
</html>